package titt.server.func;

import titt.head.HeadFactory;
import titt.head.request.MethodHead;
import titt.head.response.ResponseHead;
import titt.io.socket.TITTSocket;
import titt.key.TITTKey;
import titt.server.os.ObjectServer;
import titt.server.service.Service;
import titt.service.ServicePackage;

public class ServiceSync implements TITTFunction {

	public String getMethodName() {
		return "sync";
	}

	public void handle(TITTKey key, MethodHead head, TITTSocket ts, ObjectServer os) throws Exception {
		String packageID = head.getParams()[0].getObject();
		
		ServicePackage pack = new ServicePackage(packageID, ts.getHeadReader().readHead(), ts);

		Service ser = os.getServerDatabase().get(pack.getServiceID());
		

		if(ser == null) {
			ts.getHeadWriter().writeHead(new ResponseHead(418, HeadFactory.EMPTY));
			ts.close();
			return;
		}
			
		ser.sync(pack);
	}

}
